package com.qmth.gkwb.data.pay;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.qmth.gkwb.data.constant.OrderStatus;
import com.qmth.gkwb.data.entity.Order;
import com.qmth.gkwb.data.exception.OrderCreateFailException;
import com.qmth.gkwb.data.repository.OrderRepo;
import com.qmth.gkwb.data.repository.ZFZFMXRepo;
import com.qmth.pay.OrderFormData;
import com.qmth.pay.Sign;
import com.qmth.pay.chinapay.ChinaPaySign;
import com.qmth.pay.chinapay.ChinaPayVersion;
import com.qmth.pay.chinapay.ChinaPayOrderFormData;
import com.qmth.pay.exception.KeyErrorException;

@Component
public class OrderService {

	@Autowired Environment env;
	@Autowired private OrderRepo orderRepo;
	@Autowired private ZFZFMXRepo zfmxRepo;
	
	/**
	 * 根据考生报名信息生成银联缴费订单
	 * @param order 系统订单
	 * @return
	 * @throws KeyErrorException
	 */
	public OrderFormData createChinaPayOrder(String examId) throws OrderCreateFailException,KeyErrorException{
		List<Order> orders = orderRepo.findByExamId(examId);
		if(orders.size() == 0){
			throw new OrderCreateFailException("订单号为空，创建订单失败");
		}
		Order order = orders.get(0);
		//生成订单
		ChinaPayOrderFormData data = new ChinaPayOrderFormData();
		data.setOrdId(order.getOrderId());
		DecimalFormat df = new DecimalFormat("#######.##"); 
		int bill = (int)(Double.parseDouble(df.format(order.getMoney()))*100);
		String amt = String.format("%012d",bill);
		data.setTransAmt(amt);
		Calendar now = Calendar.getInstance();
		data.setPayDate(now.getTime());
		data.setMoney(bill);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String transDate = sdf.format(now.getTime());
		data.setTransDate(transDate);
		String path = env.getProperty("app.pay.keyPath");
		data.setBgRetUrl(env.getProperty("app.pay.bgRetUrl"));
		data.setPageRetUrl(env.getProperty("app.pay.pageRetUrl"));
		Sign sign = new ChinaPaySign();
		String merId = env.getProperty("app.pay.merId");
		data.setMerId(merId);
		String signKey = sign.encrypt(ChinaPayVersion.V_20070129,merId, data, path);
		data.setChkValue(signKey);
		return data;
	}
	
	
	/**
	 * 判断缴费完成状态：
1.查询报名库，如果有支付成功的，返回支付完成
2.如果报名库没有支付成功的，查询主数据库，如果有缴费完成，返回支付完成
	 * @param examId
	 * @return
	 */
	public boolean isOrderPaid(String examId){
		int c1 = orderRepo.countByStatusAndExamId(OrderStatus.COMPLETE,examId);
		if(c1 > 0){
			return true;
		}
		int c2 = zfmxRepo.countByExamIdAndPayOrderStatus(examId,OrderStatus.COMPLETE);
		if(c2 > 0){
			return true;
		}
		return false;
	}
	
	
	/**
	 * 生成订单：
	1.报名提交后，如果需要缴费，生成订单
	2.修改信息提交，查找主数据库支付表，如果存在只有缴费失败的订单或作废的订单；
		如果主数据库没有，查找报名数据库，如果没有或只有缴费失败或作废的订单，生成新订单
	 * @return
	 */
	public Order createOrder(){
		return null;
	}
	
	public static void main(String[] arg){
		double m = 101;
		DecimalFormat df = new DecimalFormat("#######.##"); 
		int bill = (int)(Double.parseDouble(df.format(m))*100);
		String str = String.format("%012d",bill);
		System.out.println(str);
		
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String transDate = sdf.format(now.getTime());
		System.out.println(transDate);
	}
}
